package com.ssh;

import org.junit.Test;

public class MergeTwoLists_21 {
	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode x = new ListNode(0);
        ListNode y = x;
        while (l1 != null && l2 != null) {
        	if (l1.val < l2.val) {
        		x.next = l1;
        		l1 = l1.next;
        	}else {
        		x.next = l2;
        		l2 = l2.next;
        	}
        	x = x.next;
        }
        if (l1 == null ) {
        	while (l2 != null) {
        		x.next = l2;
        		l2 = l2.next;
        		x = x.next;
        	}
        }else {
        	while (l1 != null) {
        		x.next = l1;
        		l1 = l1.next;
        		x = x.next;
        	}
        }
        return y.next;
    }
	public class ListNode {
		 int val;
		 ListNode next;
		 ListNode(int x) { val = x; }
	}
	@Test
	public void test () {
		 ListNode a = new ListNode(1);
		 ListNode b = new ListNode(2);
		 ListNode c = new ListNode(4);
		 a.next = b;
		 b.next = c;
		 ListNode d = new ListNode(1);
		 ListNode e = new ListNode(3);
		 ListNode f = new ListNode(4);
		 d.next = e;
		 e.next = f;
		 System.out.println(mergeTwoLists(a, d));
	}
	
}
